package com.shihb.creationalpatterns;

import java.util.Date;

/**
 *Description:原型模式 克隆
 *Version:1.0.0
 *@author shihb
 *@date 2020/4/21 9:52
 */
public class PrototypePattern {

  public static void main(String[] args) throws Exception {
    Date date = new Date();
    Video video = new Video("test", date);
    Video videoClone = (Video)video.clone();
    System.out.println(video);
    System.out.println(video.hashCode());
    System.out.println(videoClone);
    System.out.println(videoClone.hashCode());
    date.setTime(1500000000L);
    System.out.println("=========");
    System.out.println(video);
    System.out.println(video.hashCode());
    System.out.println(videoClone);
    System.out.println(videoClone.hashCode());

  }

}
class Video implements Cloneable{
  private String name;
  private Date date;

  public Video() {
  }

  public Video(String name, Date date) {
    this.name = name;
    this.date = date;
  }

  @Override
  public String toString() {
    return "Video{" +
        "name='" + name + '\'' +
        ", date=" + date +
        '}';
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public Date getDate() {
    return date;
  }

  public void setDate(Date date) {
    this.date = date;
  }

  @Override
  protected Object clone() throws CloneNotSupportedException {
    //浅克隆 指向同一引用对象,该对象改变,原型和克隆都跟着改变
//    return super.clone();
    // 深拷贝,引用类型也要克隆
    Object clone = super.clone();
    Video video=(Video)clone;
    video.setDate((Date)video.getDate().clone());

    return clone;

  }
}
